package 华为OD机试真题2023.进阶题;

import utils.MyUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class HJ23_快递开租建站 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/进阶题/input/23.txt"));
        MyUtil.START();
        Solution(in);
        MyUtil.END();
    }

    static void Solution(Scanner in) {
        int taskNum = in.nextInt();
        int relationsNum = in.nextInt();
        int[][] edge = new int[taskNum][taskNum];
        int[] inDgree = new int[taskNum];
        for (int i = 0; i < relationsNum; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
            edge[x][y] = 1;
            inDgree[y]++;
        }
        Queue<Integer> queue = new ArrayDeque<>();
        for (int i = 0; i < taskNum; i++) {
            if (inDgree[i] == 0) {
                queue.add(i);
            }
        }
        int res = 0;
        while (!queue.isEmpty()) {
            res++;
            List<Integer> list = new ArrayList<>();
            while (!queue.isEmpty()) {
                list.add(queue.poll());
            }
            for (int i = 0; i < list.size(); i++) {
                int now = list.get(i);
                for (int j = 0; j < taskNum; j++) {
                    if (inDgree[j] == 0) {
                        continue;
                    }
                    if (edge[now][j] == 1) {
                        inDgree[j]--;
                        if (inDgree[j] == 0) {
                            queue.add(j);
                        }
                    }
                }
            }
        }
        System.out.println(res);
    }
}
